//
//  WBMainViewController.swift
//  新浪微博
//
//  Created by shanlin on 2017/8/9.
//  Copyright © 2017年 shanlin. All rights reserved.
//

import UIKit

class WBMainViewController: UITabBarController {

    //MARK:- 私有控件
    //撰写按钮
    fileprivate lazy var composeButton : UIButton = UIButton.cz_imageButton("tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")
    //private 能够保证方法私有,仅在当前对象被访问
    // @objc 允许这个函数在运行时通过OC 的消息机制被调用
    @objc fileprivate  func composeState() {
        print("撰写微博")
        
        //测试设备横屏
        let vc = UIViewController()
        vc.view.backgroundColor = UIColor.red
        let nav = UINavigationController(rootViewController: vc)
        
        present(nav, animated: true, completion: nil)
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupChildViewController()
        setupComposeButton()


    }
    
    
    
    /*
     - portrait    : 竖屏,肖像
     - landscape   : 横屏, 风景画
     - 如果播放视频,通常是通过modal 展现的
     
     */
    
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }



}

//extension 类似OC 中的分类,在 Swift中还可以用来切分代码块
//可以吧相近功能的函数,放在一个extension中
//便于代码维护         支付宝,qq 网易新闻
//注意: 和OC的分类一样,extension 中不能定义属性
//MARK: -设置界面
extension WBMainViewController{
    
    //设置撰写按钮
    fileprivate func setupComposeButton() {
        tabBar.addSubview(composeButton)
        //设置按钮的位置
        let count = CGFloat(childViewControllers.count)
        //将向内缩进的宽度减小,能够让按钮的宽度变大,盖住容错点,防止穿帮
        let w = tabBar.bounds.width / count - 1
        print(w)
        composeButton.frame = tabBar.bounds.insetBy(dx: 2 * w, dy: 0)
        composeButton.addTarget(self, action: #selector(composeState), for: .touchUpInside)
        
    }
    
    
    //设置所有子控制器
    fileprivate func setupChildViewController() {
        
        //获取沙盒路径
        let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        let jsonPath = (docDir as NSString).appendingPathComponent("main.json")
        
        //加载data
        var data = NSData(contentsOfFile: jsonPath)
        if data == nil {
            //从bundle加载data
            let path = Bundle.main.path(forResource: "main.json", ofType: nil)
            data = NSData(contentsOfFile: path!)
            
        }
        //data一定会有一个内容,反序列化
        
        
        
        //从Bundle 加载配置的json
        // 1.路径   /2.加载NSData  /3.反序列化转化成数 组
        guard let array = try? JSONSerialization.jsonObject(with: data! as Data, options: [])as? [[String:AnyObject]] else {
            return
        }
                
        var arrayM = [UIViewController]()
        for dict in array! {
//            arrayM.append(controller(dict: dict as [String : AnyObject]))
            arrayM.append(controller(dict: dict))
        }
        //
        viewControllers = arrayM
        
        
    }
    
    private func controller(dict:[String:AnyObject]) -> UIViewController {
        //使用字典创建资格自控制器 [clsName, title, imageName]
        
        guard let title = dict["title"] as? String,
        let imageName = dict["imageName"] as?String,
        let clsName = dict["clsName"] as? String,
        let cls = NSClassFromString(Bundle.main.nameSpace() + "." + clsName) as? WBBaseViewController.Type,
        let visitorDict = dict["visitorInfo"] as? [String: String]
        
            
            else {
            return UIViewController()
        }
        let vc = cls.init()
        vc.title = title
        vc.visitorInfoDic = visitorDict
        vc.tabBarItem.image = UIImage(named: "tabbar_"+imageName)
        vc.tabBarItem.selectedImage = UIImage(named: "tabbar_"+imageName+"_selected")?.withRenderingMode(.alwaysOriginal)
        //设置tabbar 的标题字体
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: .highlighted)
        //系统默认12
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFont(ofSize: 14)], for: .normal)
        
        
        
        let nav = WBNaviationController(rootViewController: vc)
        
        return nav
        
        
        
        
        
     
    }
    
    
}
